/**   
* @Title: BeanUtilsEx.java
* @Package org.xaoyaoyao.cloud.jpa.utils
* @Description: TODO
* @author xaoyaoyao
* @date Feb 12, 2019 10:42:52 AM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.jpa.utils;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: BeanUtilsEx
 * @Description: TODO
 * @author xaoyaoyao
 * @date Feb 12, 2019 10:42:52 AM
 * 
 */
@Slf4j
public class BeanUtilsEx extends BeanUtils {

	static {
		ConvertUtils.register(new DateConvert(), java.util.Date.class);
		ConvertUtils.register(new DateConvert(), String.class);
	}

	public static void copyProperties(Object source, Object target) {
		try {
			copyProperties(source, target, getNullPropertyNames(source));
		} catch (Exception e) {
			log.error(">>>>>>>> 扩展BeanUtils.copyProperties支持data类型:" + e.getMessage(), e);
		}
	}

	public static String[] getNullPropertyNames(Object source) {
		final BeanWrapper src = new BeanWrapperImpl(source);
		java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
		Set<String> emptyNames = new HashSet<String>();
		for (java.beans.PropertyDescriptor pd : pds) {
			Object srcValue = src.getPropertyValue(pd.getName());
			if (srcValue == null)
				emptyNames.add(pd.getName());
		}
		String[] result = new String[emptyNames.size()];
		return emptyNames.toArray(result);
	}

}
